/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.ejb;


import com.mycompany.psc.entities.FileResource;
import com.mycompany.psc.entities.Resource;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.validation.ConstraintViolation;

/**
 *
 * @author A7med
 */
@Stateless
public class ResourcesManager {

    @PersistenceContext(unitName = "com.mycompany_PSC_war_1.0-SNAPSHOTPU")
    EntityManager em;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public List<Resource> getResources(String dept, int id, String reourceType, String section) {
        TypedQuery<Resource> query = em.createNamedQuery("Resource.tabResource", Resource.class);
        query.setParameter("dept", dept);
        query.setParameter("projectId", id);
        
        query.setParameter("section", "%" + section + "%");
        return query.getResultList();
    }

    

    public void saveFileResource(String rName, String deptartment, Integer id, String name, String section,String type,String path) {

        FileResource r = new FileResource();
        System.out.println("name=" + rName + " and dept=" + deptartment + " and id=" + id + " and autor=" + name + " and section=" + path);
        r.setAuthor(name);
        r.setCreationDate(new Date());
        r.setDept(deptartment);
        r.setProjectId(id);
        r.setName(rName);
        r.setSection(section);
        r.setResourceType("file_resource");
        r.setPath(path);
        r.setType(type);

        try {
            em.persist(r);
        } catch (javax.validation.ConstraintViolationException e) {
            for (ConstraintViolation v : e.getConstraintViolations()) {
                System.err.println("violated" + v.getMessage());
            }
        }
    }

    

    public void saveMultiResource(int num, String deptartment, Integer id, String name, String[] section) {

        for (int i = 0; i < num; i++) {
            int randomSection = (int) (Math.random() * (section.length));
            Resource r = new Resource();

            r.setAuthor(name);
            r.setDept(deptartment);
            r.setProjectId(id);
            r.setName(getRandomName() + i);
            r.setSection(section[randomSection]);
            em.persist(r);
        }
    }

    private String getRandomName() {
        String[] letters = {"A", "a", "B", "b", "C", "c", "d", "e", "f", "g", "h", "j", "k", "l", "s", "v", "n", "m", "x"};
        StringBuilder sb = new StringBuilder();
        int randomLength = (int) (Math.random() * 7);
        for (int i = 0; i <= randomLength; i++) {
            int raCharacter = (int) (Math.random() * (letters.length));
            sb.append(letters[raCharacter]);
        }
        return sb.toString();
    }
    public void deleteResource(Resource r){
        Resource removable=em.getReference(Resource.class, r.getId());
        em.remove(removable);
    }
    public String getPath(Resource r){
        String path="";
        Resource re=em.getReference(Resource.class, r.getId());
        if(re instanceof FileResource){
            FileResource f=(FileResource) re;
            path=f.getPath();
        }
        return path;
    }
    public String getSection(Resource r){
        String path="";
        Resource re=em.getReference(Resource.class, r.getId());
        if(re instanceof FileResource){
            FileResource f=(FileResource) re;
            path=f.getSection();
        }
        return path;
    }
}
